﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Model;
using MsDAL;

namespace UIMarker
{
    public partial class FrmFunSource : Form
    {
        private string action = "A";
        public FrmFunSource(string k)
        {
            InitializeComponent();
        }

        private void tspBtnAdd_Click(object sender, EventArgs e)
        {
            foreach (Control c in this.Controls)
            {
                if (!(c is TextBox) || c.Tag != null) continue;

                ((TextBox)c).Clear();
                c.Enabled = true;
            }

            action = "A";
        }

        private void tspBtnSave_Click(object sender, EventArgs e)
        {
            Sys0a80DAL dal = new Sys0a80DAL();
            CommonDAL comdal = new CommonDAL();
            Sys0a80 model = new Sys0a80();
            UIHelper.FillModelFromUI(model, this.Controls);
            model.Isprivate = "N";
            model.Mdate = DateTime.Now;
            model.Muser = "SYSTEM";

            List<SqlApp> lst = new List<SqlApp>();
            if ("A" == this.action)
            {
                lst.Add(dal.Add(model));
            }
            else if ("M" == this.action)
            {
                lst.Add(dal.Update(model));
            }

            string errmsg = comdal.SaveData(lst);
            if (string.IsNullOrEmpty(errmsg))
            {
                MessageBox.Show("保存OK!", "提示");
                tspBtnAdd_Click(null, null);
            }
            else
            {
                MessageBox.Show(errmsg, "提示");
            }
        }

        private void tspEdit_Click(object sender, EventArgs e)
        {
            FrmFunSourceSlt slt = new FrmFunSourceSlt();
            if (slt.ShowDialog() == DialogResult.OK)
            {
                UIHelper.FillUIFromModel(slt.GetSelectedData(), this.Controls);
                this.fnkey.Enabled = false;
                this.action = "M";
            }
        }

        private void tspBtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
